package com.hykj.xxltest.utils;

import org.apache.commons.lang.StringUtils;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.*;

public class InnelUtil {


    public static String parseIntListToString (List<Integer> arr) {

        StringBuilder stringBuilder=new StringBuilder();

        for (int i = 0; i < arr.size(); i++) {
            stringBuilder.append(arr.get(i));
            stringBuilder.append(",");
        }
        if(stringBuilder.length()>0){
            stringBuilder.deleteCharAt(stringBuilder.length()-1);
        }
        return stringBuilder.toString();
    }

    public static List<Integer> parseStirngToIntList(String arr) {

        List intList = new ArrayList<>();
        int[] arrI = parseStringToInt(arr);
        for (int i = 0; i < arrI.length; i++) {
            intList.add(arrI[i]);
        }
        return intList;
    }

    public static int[] parseStringToInt(String[] arr) {
        int[] arrI = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            if (StringUtils.isEmpty(arr[i])) {
                arrI[i] = 0;
            } else {
                arrI[i] = Integer.parseInt(arr[i]);
            }

        }
        return arrI;
    }

    /**
     * 用，分割转int
     *
     * @param arrS
     * @return
     */
    public static int[] parseStringToInt(String arrS) {
        if (StringUtils.isEmpty(arrS)) {
            return new int[]{};
        }
        return parseStringToInt(arrS.split(","));
    }

    /**
     * 判断是否有重复原素
     */
    public static boolean containsDuplicate(int[] nums) {
        Set<Integer> numSet = new HashSet<Integer>();
        for (int i = 0; i < nums.length; i++) {
            if (numSet.contains(nums[i])) {
                return true;
            } else {
                numSet.add(nums[i]);
            }
        }
        return false;
    }


    /**
     * 获得一个常用的HashMap
     */
    public static HashMap<String, Object> getHashMap() {

        return new HashMap<>(16);
    }


    static public String filterOffUtf8Mb4(String text) {
        byte[] bytes = new byte[0];
        try {
            bytes = text.getBytes("utf-8");
        } catch (UnsupportedEncodingException e) {
        }
        ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
        int i = 0;
        while (i < bytes.length) {
            short b = bytes[i];
            if (b > 0) {
                buffer.put(bytes[i++]);
                continue;
            }
            b += 256;
            if ((b ^ 0xC0) >> 4 == 0) {
                buffer.put(bytes, i, 2);
                i += 2;
            } else if ((b ^ 0xE0) >> 4 == 0) {
                buffer.put(bytes, i, 3);
                i += 3;
            } else if ((b ^ 0xF0) >> 4 == 0) {
                i += 4;
            } else {
                break;
            }

        }
        buffer.flip();
        try {
            return new String(buffer.array(), "utf-8");
        } catch (UnsupportedEncodingException e) {

        }
        return "";
    }
}
